package com.st.mall.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.mall.product.mapper.ICateMapper;
import com.st.mall.product.model.CateModel;
import com.st.mall.product.service.ICateService;

@Service
public class CateServiceImpl implements ICateService {
	
	@Autowired
	private ICateMapper mapper;

	/**
	 * A:编号已存在
	 * B:添加成功
	 * C:添加失败
	 */
	@Override
	public String insert(CateModel cateModel) {
		// TODO Auto-generated method stub
		String code = cateModel.getCode();
		CateModel model1 = mapper.selectId(code);
		if(model1 != null) {
			return "A";		// 编号重复，该一级分类已存在
		}
		int result = mapper.insert(cateModel);
		if(result == 1) {
			return "B";		// 添加成功
		}
		return "C";
	}

	@Override
	public CateModel selectId(String code) {
		CateModel model = mapper.selectId(code);
		return model;
	}

	@Override
	public int delete(String code) {
		int result = mapper.delete(code);
		return result;
	}

	@Override
	public int update(CateModel cateModel) {
		int result = mapper.update(cateModel);
		return result;
	}

	@Override
	public List<CateModel> selectAll(String parentCode) {
		List<CateModel> list = mapper.selectAll(parentCode);
		return list;
	}

	@Override
	public List<CateModel> selectModel(CateModel cateModel) {
		String code = cateModel.getCode();
		if (code != null) {
			cateModel.setCode("%" + code + "%");
		}
		String name = cateModel.getName();
		if (name != null) {
			cateModel.setName("%" + name + "%");
		}
		List<CateModel> list = mapper.selectModel(cateModel);
		return list;
	}

	@Override
	public int selectCount(CateModel cateModel) {
		int result = mapper.selectCount(cateModel);
		return result;
	}
	
}
